#include "stdafx.h"
#include "TextureManager.h"


static const char* WHITE_TEXTURE_PATH = "../Engine/Textures/white.png";

TextureId CTextureManager::white_texture = 0;


CTextureManager::CTextureManager()
: m_lastId(0)
{
	//white_texture = CreateTexture(WHITE_TEXTURE_PATH, 0, 0, 0, eTexFormat_A8R8G8B8, false);
}

CTextureManager::~CTextureManager()
{
	//	release textures
	for (uint i = 0; i < m_textures.size(); ++i)
		delete m_textures[i];
}

TextureId CTextureManager::CreateTexture(uint width, uint height, uint levels, ETextureFormat format, bool dynamic)
{
	ITexture* pTexture = g_pRenderAPI->CreateTexture(width, height, levels, format, dynamic);
	return AddTexture(pTexture);
}

bool CTextureManager::DeleteTexture(TextureId id)
{
	TexturesList::iterator it = m_textures.find(id);
	if (it != m_textures.end())
	{
		delete it->second;
		m_textures.erase(it);
		return true;
	}
	return false;
}

ITexture*	CTextureManager::GetTexture(TextureId id)
{
	return stl::find_in_map(m_textures, id, nullptr);
}

TextureId CTextureManager::AddTexture(ITexture* texture)
{
	assert(texture);

	m_textures[++m_lastId] = texture;
	return m_lastId;
}

TextureId CTextureManager::CreateTexture(const char* filepath, uint width, uint height, uint levels, ETextureFormat format, bool dynamic)
{
	ITexture* pTexture = g_pRenderAPI->CreateTexture(filepath, width, height, levels, format, dynamic);
	return AddTexture(pTexture);
}